package ckw.gui.wizards;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ckw.database.VotingSubcard;
import ckw.database.VotingSubcardOption;

public class WzdElectionDlgVotingSubcardOptionAddEdit extends Dialog {
	private VotingSubcard votingSubcard;
	private VotingSubcardOption votingSubcardOption;
	private Text txtNazwa;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param votingSubcard2 
	 */
	public WzdElectionDlgVotingSubcardOptionAddEdit(Shell parentShell, VotingSubcard votingSubcard, VotingSubcardOption votingSubcardOption) {
		super(parentShell);
		
		this.votingSubcard = votingSubcard; 
		this.votingSubcardOption = votingSubcardOption;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 2;

		Label lblNazwa = new Label(container, SWT.NONE);
		lblNazwa.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNazwa.setText("Nazwa:");

		txtNazwa = new Text(container, SWT.BORDER);
		txtNazwa.setText(votingSubcardOption.getName() == null ? "" : votingSubcardOption.getName());
		txtNazwa.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
	
	@Override
	protected void okPressed() {
		votingSubcardOption.setName(txtNazwa.getText());
		// Jesli nie zawiera, to dodajemy, jesli zawiera, po prostu edytujemy
		if(!votingSubcard.getVotingSubcardOptions().contains(votingSubcardOption)) {
			votingSubcard.getVotingSubcardOptions().add(votingSubcardOption);
		}
		
		super.okPressed();
	}
}
